import pygame
from pygame import QUIT

from Player import Player
from Tiled import Tiled
from data import config, mapData, color
from locals import updates, events, playerPath

pygame.init()
window = pygame.display.set_mode(config['windowSize'])
clock = pygame.time.Clock()

playerGroup = pygame.sprite.Group()
player = Player()

mapGroup = pygame.sprite.Group()

for x, row in enumerate(mapData):
    for y, item in enumerate(row):
        tiled = Tiled(item)
        tiled.rect.topleft = (0 + (config['tiledSize'][0] + config['tiledOffset'][0]) * y,
                              0 + (config['tiledSize'][1] + config['tiledOffset'][1]) * x)
        mapGroup.add(tiled)
        if item == 2:
            player.rect.center = tiled.rect.center
        if item == 3:
            playerPath.append(tiled.rect.center)
playerGroup.add(player)


def main():
    while True:
        tick = clock.tick(60)
        for event in pygame.event.get():
            for _event in events:
                _event['callable'](_event)
            if event.type == QUIT:
                exit(-1)

        mapGroup.draw(window)
        mapGroup.update()
        playerGroup.draw(window)
        playerGroup.update()

        for update in updates:
            update['callable'](tick)

        pygame.display.update()
        window.fill(color['black'])


if __name__ == '__main__':
    main()
